package com.commerceerp.system.controller;

import com.commerceerp.system.entity.ShopAmazon;
import com.commerceerp.system.entity.ShopEbay;
import com.commerceerp.system.entity.ShopWish;
import com.commerceerp.system.service.*;
import com.commerceerp.system.util.ApiResult;
import com.commerceerp.system.util.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * Created by Administrator on 2018/6/11/011.
 */
@Api(description = "店铺管理", value = "店铺管理页面")
@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    private ShopService shopService;

    @Autowired
    private LoginService loginService;

    @Autowired
    private WishService wishService;

    @Autowired
    private AmazonService amazonService;

    @Autowired
    private EbayService ebayService;

    @ApiOperation(value = "wish店铺")
    @RequestMapping(value = "/wishFindList", method = RequestMethod.POST)
    public ApiResult wishFindList(HttpServletRequest request){
        String token = request.getHeader("token");
        ApiResult to = loginService.checkToken(token);
        if(to.getCode() != 200){
            return to;
        }
        return shopService.wishFindList((String) to.getData());
    }

    //wish店铺编辑
    @RequestMapping(value = "/formWish", method = RequestMethod.POST)
    @ApiOperation(value = "wish店铺添加/修改")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "clientId", value = "客户端Id", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "clientSecret", value = "客户端秘钥", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "shopName", value = "店铺名称", required = true, dataType = "String"),
    })
    public ApiResult formWish(ShopWish shopWish,HttpServletRequest request){
        String token = request.getHeader("token");
        ApiResult to = loginService.checkToken(token);
        if(to.getCode() != 200){
            return to;
        }
        shopWish.setShopUserId((String) to.getData());
        return shopService.formWish(shopWish);
    }

    //wish店铺删除
    @ApiOperation(value = "wish店铺删除")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id", required = false, dataType = "String"),
    })
    @RequestMapping(value = "/delWish", method = RequestMethod.POST)
    public ApiResult delWish(String id,HttpServletRequest request){
        String token = request.getHeader("token");
        ApiResult to = loginService.checkToken(token);
        if(to.getCode() != 200){
            return to;
        }
        return shopService.delWish(id,(String) to.getData());
    }

    //wish店铺手动同步订单
    @ApiOperation(value = "wish店铺手动同步订单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id", required = false, dataType = "String"),
    })
    @RequestMapping(value = "/getWishOrder", method = RequestMethod.POST)
    public ApiResult getWishOrder(HttpServletRequest request){
        String token = request.getHeader("token");
        ApiResult to = loginService.checkToken(token);
        if(to.getCode() != 200){
            return to;
        }
        return wishService.getWishOrder((String) to.getData());
    }

    //Amazon店铺
    @ApiOperation(value = "Amazon店铺")
    @RequestMapping(value = "/amazonFindList", method = RequestMethod.POST)
    public ApiResult amazonFindList(HttpServletRequest request){
        String token = request.getHeader("token");
        ApiResult to = loginService.checkToken(token);
        if(to.getCode() != 200){
            return to;
        }
        return shopService.amazonFindList((String) to.getData());
    }

    //Amazon开户站
    @ApiOperation(value = "Amazon开户站")
    @RequestMapping(value = "/amazonEndpoint", method = RequestMethod.POST)
    public ApiResult amazonEndpoint(HttpServletRequest request){
        String token = request.getHeader("token");
        ApiResult to = loginService.checkToken(token);
        if(to.getCode() != 200){
            return to;
        }
        return shopService.amazonEndpoint();
    }

    //Amazon店铺编辑
    @ApiOperation(value = "Amazon店铺编辑/修改")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "merchantId", value = "商家编号", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "awsAccessKeyId", value = "访问编号", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "secretKey", value = "秘钥", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "accountWeb", value = "开户站", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "shopName", value = "店铺名称", required = true, dataType = "String"),
    })
    @RequestMapping(value = "/formAmazon", method = RequestMethod.POST)
    public ApiResult formAmazon(ShopAmazon shopAmazon,HttpServletRequest request){
        String token = request.getHeader("token");
        ApiResult to = loginService.checkToken(token);
        if(to.getCode() != 200){
            return to;
        }
        shopAmazon.setShopUserId((String) to.getData());
        return shopService.formAmazon(shopAmazon);
    }

    //Amazon店铺删除
    @ApiOperation(value = "Amazon店铺删除")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id", required = false, dataType = "String"),
    })
    @RequestMapping(value = "/delAmazon", method = RequestMethod.POST)
    public ApiResult delAmazon(String id,HttpServletRequest request){
        String token = request.getHeader("token");
        ApiResult to = loginService.checkToken(token);
        if(to.getCode() != 200){
            return to;
        }
        return shopService.delAmazon(id,(String) to.getData());
    }

    //Amazon店铺手动同步订单
    @ApiOperation(value = "Amazon店铺手动同步订单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id", required = false, dataType = "String"),
    })
    @RequestMapping(value = "/getAmazonOrder", method = RequestMethod.POST)
    public ApiResult getAmazonOrder(HttpServletRequest request){
        String token = request.getHeader("token");
        ApiResult to = loginService.checkToken(token);
        if(to.getCode() != 200){
            return to;
        }
        String date = DateUtil.getAfterDate(DateUtil.getYMDHMSDate(),-3);
        return amazonService.getOrder((String) to.getData(),date);
    }

    @RequestMapping(value = "/getImgUrl", method = RequestMethod.POST)
    public ApiResult getImgUrl(){
        amazonService.getImgUrl();
        return null;
    }

    //ebay店铺创建
    @ApiOperation(value = "ebay店铺创建")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "shopName", value = "店铺名称", required = true, dataType = "String"),
    })
    @RequestMapping(value = "/formEbay", method = RequestMethod.POST)
    public ApiResult formEbay(ShopEbay shopEbay, HttpServletRequest request){
        String token = request.getHeader("token");
        ApiResult to = loginService.checkToken(token);
        if(to.getCode() != 200){
            return to;
        }
        shopEbay.setShopUserId((String) to.getData());
        return shopService.formEbay(shopEbay);
    }

    @ApiOperation(value = "验证ebay门店是否授权")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "sessionID", value = "sessionID", required = true, dataType = "String"),
    })
    @RequestMapping(value = "/ebay/vailCode", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult vailCode(String sessionID){

        return shopService.vailCode(sessionID);
    }

    @ApiOperation(value = "ebay店铺手动同步订单")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "店铺id", required = false, dataType = "String"),
    })
    @RequestMapping(value = "/ebay/order", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getEbayOrders(String id,HttpServletRequest request){
        String token = request.getHeader("token");
        ApiResult to = loginService.checkToken(token);
        if(to.getCode() != 200){
            return to;
        }
        String date = DateUtil.getYMDHMSDate();
        date = DateUtil.getAfterDate(date,1);
        String date2 = DateUtil.getAfterDate(date,-3);
        return ebayService.getEbayOrders(id,(String)to.getData(),date,date2);
    }

    @ApiOperation(value = "ebay店铺添加商品")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "店铺id", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "name", value = "名称", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "description", value = "简介", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "sku", value = "sku", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "pic", value = "图片，多张中间,隔开", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "price", value = "单价", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "categoryId", value = "分类id", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "title", value = "标题", required = true, dataType = "String"),
    })
    @RequestMapping(value = "/ebay/addItem", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult addItem(String id,String name,String description,String sku,String pic,double price,String categoryId,String title,HttpServletRequest request){
        String token = request.getHeader("token");
        ApiResult to = loginService.checkToken(token);
        if(to.getCode() != 200){
            return to;
        }
        return ebayService.addItem(id,name,description,sku,pic,price,categoryId,title);
    }

    @ApiOperation(value = "ebay分类")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "父级分类id（不传默认为0）", required = false, dataType = "String"),
    })
    @RequestMapping(value = "/ebay/getCategory", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult getCategory(String id){

        return ebayService.getCategory(id);
    }

    @ApiOperation(value = "ebay店铺手动同步消息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "店铺id", required = false, dataType = "String"),
    })
    @RequestMapping(value = "/ebay/synMessage", method = RequestMethod.POST)
    @ResponseBody
    public ApiResult synMessage(String id,HttpServletRequest request){
        String token = request.getHeader("token");
        ApiResult to = loginService.checkToken(token);
        if(to.getCode() != 200){
            return to;
        }
        String date = DateUtil.getAfterDate(DateUtil.getYMDHMSDate(),-3);
        String date2 = DateUtil.getAfterDate(DateUtil.getYMDHMSDate(),1);
        return ebayService.getMessage(id,(String) to.getData(),date,date2);
    }

    //ebay店铺
    @ApiOperation(value = "ebay店铺")
    @RequestMapping(value = "/ebayFindList", method = RequestMethod.POST)
    public ApiResult ebayFindList(HttpServletRequest request){
        String token = request.getHeader("token");
        ApiResult to = loginService.checkToken(token);
        if(to.getCode() != 200){
            return to;
        }
        return shopService.ebayFindList((String) to.getData());
    }

    //ebay店铺删除
    @ApiOperation(value = "ebay店铺删除")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "id", value = "id", required = false, dataType = "String"),
    })
    @RequestMapping(value = "/delEbay", method = RequestMethod.POST)
    public ApiResult delEbay(String id,HttpServletRequest request){
        String token = request.getHeader("token");
        ApiResult to = loginService.checkToken(token);
        if(to.getCode() != 200){
            return to;
        }
        return shopService.delEbay(id,(String) to.getData());
    }

}
